<?php
namespace Apipad\Controller;
use Think\Controller;

class UserTuanController extends ComController{

    //前置判断
    function _before_index(){
        $return['code'] = 101;
        $return['msg'] = '请先输入二级密码才能进入';
        //2级有列表权限
        $post = I('post.');
        if(!$post['token_cc']){
            $this->ajaxSucReturn($return);
        }else{
             $uinfocc = is_login_cctt(I('token_cc'));
             if(!$uinfocc){
                $this->ajaxSucReturn($return);
             }
        }
    
    }

    //删除前置
    function _before_vdel(){
        $return['code'] = 101;
        $return['msg'] = '请先输入二级密码才能进入';
        //2级有列表权限
        $post = I('post.');
        if(!$post['token_cc']){

            $this->ajaxSucReturn($return);
        }else{
             $uinfocc = is_login_cctt(I('token_cc'));
             if(!$uinfocc){
                $this->ajaxSucReturn($return);
             }
        }
    
    }

    //添加后置
    function add(){
        $model = D(CONTROLLER_NAME);
        $model->startTrans();
        $_POST['uid'] = UID;
        $data = $model->create();
        if (false === $data) {
            $this->ajaxErrReturn($model->getError());
        }
        foreach($data as $field=>$val){
            if(is_array($val)){
                //数组转化为字符串
                $data[$field] = implode(',',$val);
            }
        }
        //保存当前数据对象
        $id = $model->add($data);
        if(!$id){
           $this->ajaxSucReturn('添加失败'); 
        }
        $model = M('user_tuan');
        $wdata['id'] = $id;
        $company_no = 'TT'.str_pad($id, 4, "0", STR_PAD_LEFT);
        $sdata['company_no'] = $company_no;
        $list2 = $model->where($wdata)->save($sdata);
        if ($list2 !== false) { //保存成功
            $model->commit();
            $list = $model->where(['id'=>$id,'uid'=>UID])->find();
            $data['msg'] = '新增成功';
            $data['data'] = $list;
            $this->ajaxSucReturn($data);
        } else {
            //失败提示
            $this->ajaxErrReturn($model->getError());
        } 

    }

    //编辑
    public function edit(){
        $detail = I('');
        $id = $detail['id'];
        $company = $detail['company'];
        $contacts = $detail['contacts'];
        $tel = $detail['tel'];
        if(empty($id)){
            $data['code'] = 0;
            $data['msg'] = '公司ID不能为空';
            exit(json_encode($data));
        }
        if(empty($company)){
            $data['code'] = 0;
            $data['msg'] = '请填写联系人';
            exit(json_encode($data));
        }
        if(empty($tel)){
            $data['code'] = 0;
            $data['msg'] = '请填写联系电话';
            exit(json_encode($data));
        }
        $model = D('UserTuan');
        $s_data['company'] = $company;
        $s_data['contacts'] = $contacts;
        $s_data['tel'] = $tel;
        $result = $model->where(['id'=>$id,'uid'=>UID])->save($s_data);
        $info = $model->where(['id'=>$id,'uid'=>UID])->find();
        if($result){
            $data['code'] = 200;
            $data['msg'] = '编辑成功';
            $data['data'] = $info;
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '编辑失败';
            exit(json_encode($data));
        }
    }

    //公司检测
    public function company_check(){
        $model = D('UserTuan');
        $info = $model->where(['uid'=>UID,'is_delete'=>0,'status'=>0])->find();
        if($info){
            $data_cc['code'] = 200;
            $data_cc['msg'] = '存在公司信息';
            $data_cc['data']['status'] = 1;
            $data_cc['data']['info'] = $info;
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 200;
            $data_cc['msg'] = '不存在公司信息';
            $data_cc['data']['status'] = 2;
            $data_cc['data']['info'] = [];
            exit(json_encode($data_cc));
        }
    }
    //模板下载
    function export(){
        $url="https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/ipad/assets/%E5%9B%A2%E4%BD%93%E5%AE%9A%E5%88%B6%E6%A8%A1%E6%9D%BF.xlsx";
        $data_cc['code'] = 200;
        $data_cc['msg'] = '查询成功';
        $data_cc['data'] = $url;
        exit(json_encode($data_cc));
    }

    //导入Excel
    function import(){
        $excel_path = I('excel_path');
        $number = I('number');
        if(!$excel_path){
            $data_cc['code'] = 0;
            $data_cc['msg'] = 'excel地址必须！';
            exit(json_encode($data_cc));
        }
        $path = UID.'_attn.xlsx';//考勤excel
        file_put_contents($path, file_get_contents($excel_path));
        $exc_data = importExcel($path);
        $exc_count = count($exc_data);
        $exc_count = $exc_count-2;
        if($exc_count == 2){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '导入数据不能为空';
            exit(json_encode($data_cc));
        }
 
        if($exc_count>$number){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '超出部门人数,请修改部门人数';
            exit(json_encode($data_cc));
        }
        $data = [];
        $num =0;
        foreach($exc_data as $key=>$val){
            if($key==0 ||$key == 1){
                continue;
            }
            $data[$num]['names'] = $val[0];
            $data[$num]['age'] = $val[1];
            $data[$num]['sex'] = $val[2] == '男'?1:0;
            $data[$num]['height'] = $val[3];
            $data[$num]['weight'] = $val[4];
            $data[$num]['number'] = $val[5];
            $data[$num]['remark'] = $val[6];
            $data[$num]['xmcm'] = $val[7];
            $data[$num]['xfcm_linwei'] = $val[8];
            $data[$num]['xfcm_qyc'] = $val[9];
            $data[$num]['xfcm_hzc'] = $val[10];
            $data[$num]['xfcm_cyxw'] = $val[11];
            $data[$num]['xfcm_tuiwei'] = $val[12];
            $data[$num]['xfcm_zw'] = $val[13];
            $data[$num]['xkcm_cyyw'] = $val[14];
            $data[$num]['xfcm_jk'] = $val[15];
            $data[$num]['xfcm_xc'] = $val[16];
            $data[$num]['xfcm_xw'] = $val[17];
            $data[$num]['xkcm_xiabai'] = $val[18];
            $data[$num]['xkcm'] = $val[19];
            $data[$num]['xkcm_cpyw'] = $val[20];
            $data[$num]['xkcm_cytw'] = $val[21];
            $data[$num]['xkcm_hd'] = $val[22];
            $data[$num]['xkcm_qkc'] = $val[23];
            $data[$num]['xkcm_jiaokou'] = $val[24];
            $data[$num]['id'] = 0;
            $num++;
        }
        unlink($path);
        $data_cc['code'] = 200;
        $data_cc['msg'] = '导入成功';
        $data_cc['data'] = $data;
        exit(json_encode($data_cc));
    }
    //团体定制信息提交
    public function ttdz_sumbit(){
        $detail = I('');
        $company_id = $detail['company_id'];
        if(empty($company_id)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '公司ID不能为空';
            exit(json_encode($data_cc));
        }
        $model = D('UserTuan');
        $info = $model->where(['uid'=>UID,'id'=>$company_id,'is_delete'=>0,'status'=>0])->find();
        if(empty($info)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = '公司信息不存在';
            exit(json_encode($data_cc));
        }
        $res = $model->where(['uid'=>UID,'id'=>$company_id,'is_delete'=>0,])->save(['status'=>1,'qr_time'=>time()]);
        if($res){
            $data_cc['code'] = 200;
            $data_cc['msg'] = '提交成功';
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = '提交失败';
            exit(json_encode($data_cc));
        }
    }
}